<?php
/**
 *  Create On 2014-12-03
 *  Author yiwei
 *  QQ:1006629314
 **/
class TransController extends Controller{
	private $uploadpath;
	
	public function __construct() {
		parent::__construct ();
		$this->view->web_host = $this->getRequest ()->hostUrl;
		$this->view->web_app_url = $this->getRequest ()->hostUrl . "/howdomanage.php";
		$this->uploadpath ='common/upload/excel/';
	}
	
	public function index(){
		$this->view->display('trans.htm');
	}
	
	public function uploadExcel(){
		$type = $this->getRequest()->get('type');
		
		//加载上传类
		$this->getApp()->loadUtilClass("uploadutil");
		//设置上传类参数数组的值
		$fileName=$type.time ().rand(1000,9999);
		//设置上传类的参数（参数为数组）
		$upPara=array(
				'allowType'=>array("xls","xlsx"),
				'maxsize'=>"100000000",
				'newname'=>$fileName,
				'filepath'=>$this->uploadpath
		);
		$up=new uploadutil($upPara);
		$upExcel=$up->uploadFile("excel");
		if(!$upExcel) echo $up->getErrorMsg();
		
		require_once 'common/libs/PHPExcel/Classes/PHPExcel.php';
		
		$file = $fileName.".xls";
		if($type == 'class'){
			$this->_addClass($file);
		}else if($type == 'teacher'){
			$this->_addTeacher($file);
		}else if($type == 'student'){
			$this->_addClassStudent($file);
		}else if($type == 'period'){
			$this->_addPeriod($file);
		}else{
			
		}
	}
	
	private function _addClass($fileName){
	
		$begin = microtime(TRUE);
		$filePath = $this->uploadpath.$fileName;
		$PHPExcel = new PHPExcel();
		$PHPReader = new PHPExcel_Reader_Excel2007();
		if(!$PHPReader->canRead($filePath))
		{
			$PHPReader = new PHPExcel_Reader_Excel5();
			if(!$PHPReader->canRead($filePath))
			{
				echo '未发现Excel文件！';
				return;
			}
		}

		$PHPExcel = $PHPReader->load($filePath);
		$currentSheet = $PHPExcel->getSheet(0);

		$allRow = $currentSheet->getHighestRow();
	
		echo '共有'.$allRow.'行<br/>';
		$end = microtime(TRUE);
		echo '读入消耗了'.($end-$begin).'秒<br/>';
		$begin = microtime(TRUE);
		
		$trans = new trans();
		$flag = true;
		$str = '';
		for($currentRow = 2;$currentRow<$allRow;$currentRow++)
		{	
			$code = $currentSheet->getCell('A'.$currentRow)->getValue();
			//$name = $currentSheet->getCell('D'.$currentRow)->getValue();
			//$start = $currentSheet->getCell('U'.$currentRow)->getValue();
			//$end = $currentSheet->getCell('V'.$currentRow)->getValue();
			$test = $trans->testClass($code);
			if($test){
				$flag = false;
				$str .='警告：表格第'.$currentRow.'行记录班级已存在<br/>';
			}
		}
		if(true){
			for($currentRow = 2;$currentRow<$allRow;$currentRow++)
			{
				$code = $currentSheet->getCell('A'.$currentRow)->getValue();
				$name = $currentSheet->getCell('O'.$currentRow)->getValue();
				$start = $currentSheet->getCell('U'.$currentRow)->getValue();
				$end = $currentSheet->getCell('V'.$currentRow)->getValue();
				$amount = $currentSheet->getCell('Y'.$currentRow)->getValue();
				$create_user_id=1;
				$result = $trans->addClass($code, $name, $start, $end,$amount, $create_user_id);
				echo 'trans->'.$code.$name.$start.$end;
			
				echo $result.'<br/>';
			}
		}else{
			echo $str;
		}
		$end = microtime(TRUE);
		echo '检测及执行消耗了'.($end-$begin).'秒<br/>';
	}
	
	private function _addTeacher($fileName){
		$begin = microtime(TRUE);
		$filePath = $this->uploadpath.$fileName;
		$PHPExcel = new PHPExcel();
		$PHPReader = new PHPExcel_Reader_Excel2007();
		if(!$PHPReader->canRead($filePath))
		{
			$PHPReader = new PHPExcel_Reader_Excel5();
			if(!$PHPReader->canRead($filePath))
			{
				echo '未发现Excel文件！';
				return;
			}
		}
		
		$PHPExcel = $PHPReader->load($filePath);
		$currentSheet = $PHPExcel->getSheet(0);
		
		$allRow = $currentSheet->getHighestRow();
		
		echo '共有'.$allRow.'行<br/>';
		$end = microtime(TRUE);
		echo '读入消耗了'.($end-$begin).'秒<br/>';
		$begin = microtime(TRUE);
		
		$trans = new trans();
		$flag = true;
		$str = '';
		for($currentRow = 2;$currentRow<$allRow;$currentRow++)
		{
		
			$tea_code = $currentSheet->getCell('A'.$currentRow)->getValue();
			//$name = $currentSheet->getCell('B'.$currentRow)->getValue();
			//$phone = $currentSheet->getCell('H'.$currentRow)->getValue();
			
			$test= $trans->testTeacher($tea_code);
			if($test){
				$flag = false;
				$str .='警告：表格第'.$currentRow.'行记录教师已存在<br/>';
			}
		}
		if($flag){
			for($currentRow = 2;$currentRow<$allRow;$currentRow++)
			{
				$tea_code = $currentSheet->getCell('A'.$currentRow)->getValue();
				$name = $currentSheet->getCell('B'.$currentRow)->getValue();
				$phone = $currentSheet->getCell('H'.$currentRow)->getValue();
				$result = $trans->addTeacher($tea_code, $name, $phone);
				echo 'trans->'.$tea_code.$name.$phone.'->'.$result.'<br/>';
			}
			}else {
				echo $str;
			}
			$end = microtime(TRUE);
			echo '检测及执行消耗了'.($end-$begin).'秒<br/>';
	}
	
	private function _addClassStudent($fileName){
		$begin = microtime(TRUE);
		$filePath = $this->uploadpath.$fileName;
		$PHPExcel = new PHPExcel();
		$PHPReader = new PHPExcel_Reader_Excel2007();
		if(!$PHPReader->canRead($filePath))
		{
			$PHPReader = new PHPExcel_Reader_Excel5();
			if(!$PHPReader->canRead($filePath))
			{
				echo '未发现Excel文件！';
				return;
			}
		}

		$PHPExcel = $PHPReader->load($filePath);
		$currentSheet = $PHPExcel->getSheet(0);
	
		$allRow = $currentSheet->getHighestRow();
	
		echo '共有'.$allRow.'行<br/>';
		$end = microtime(TRUE);
		echo '读入消耗了'.($end-$begin).'秒<br/>';
		$begin = microtime(TRUE);
		
		$trans = new trans();
		$flag = true;
		$str = '';
		for($currentRow = 2;$currentRow<$allRow;$currentRow++)
		{
		
			$class_code = $currentSheet->getCell('A'.$currentRow)->getValue();
			$stu_code = $currentSheet->getCell('D'.$currentRow)->getValue();
			$name = $currentSheet->getCell('E'.$currentRow)->getValue();
			$test1 = $trans->testClass($class_code);
			$test2 = $trans->testStudent($stu_code);
			if(!$test1){
				$flag = false;
				$str .= '错误：表格第'.$currentRow.'行记录班级不存在,请先导入编码为 '.$class_code.' 的班级<br/>';
			}
			if($test2){
				$flag = false;
				$str .= '警告：表格第'.$currentRow.'行记录学生已存在<br/>';
			}
		}
		if(true){
			for($currentRow = 2;$currentRow<$allRow;$currentRow++)
			{
		
				$class_code = $currentSheet->getCell('A'.$currentRow)->getValue();
				$stu_code = $currentSheet->getCell('D'.$currentRow)->getValue();
				$name = $currentSheet->getCell('E'.$currentRow)->getValue();
				$phone = $currentSheet->getCell('AK'.$currentRow)->getValue();
				echo 'trans->'.$class_code.'->'.$stu_code.'->'.$name;
				$result1 = $trans->addStudent($stu_code, $name,$phone);
				echo '->addStu:'.$result1;
				$test1 = $trans->testClass($class_code);
				$test2 = $trans->testStudent($stu_code);
				$result2 = $trans->bindClassStudent($test1, $test2);
		
				echo '->bindClassStu:'.$result2.'<br/>';
			}
	
			echo $str;
		}
		$end = microtime(TRUE);
		echo '检测及执行消耗了'.($end-$begin).'秒<br/>';
	}
	
	private function _addPeriod($fileName){
		$begin = microtime(TRUE);
		$filePath = $this->uploadpath.$fileName;
	
		$cacheMethod = PHPExcel_CachedObjectStorageFactory::cache_to_discISAM;
		PHPExcel_Settings::setCacheStorageMethod($cacheMethod);
	
		$PHPExcel = new PHPExcel();
		$PHPReader = new PHPExcel_Reader_Excel2007();
		if(!$PHPReader->canRead($filePath))
		{
			$PHPReader = new PHPExcel_Reader_Excel5();
			if(!$PHPReader->canRead($filePath))
			{
				echo '未发现Excel文件！';
				return;
			}
		}

		$PHPExcel = $PHPReader->load($filePath);
		$currentSheet = $PHPExcel->getSheet(0);

		$allRow = $currentSheet->getHighestRow();
	
		echo '共有'.$allRow.'行。';
		$end = microtime(TRUE);
		echo '读入消耗了'.($end-$begin).'秒。<br/>';
		$begin = microtime(TRUE);
		
		$trans = new trans();
		$flag = 1;
		/*
		 * 能否导入标识：>0可以导入，否则不能导入
		 * 1：正常导入
		 * 2：覆盖导入
		 * 0：不能导入
		 * -1：部分导入
		 */
		$str = '';
		for($currentRow = 2;$currentRow<$allRow;$currentRow++)
		{
			$section = $currentSheet->getCell('A'.$currentRow)->getValue();
			$class_code = $currentSheet->getCell('C'.$currentRow)->getValue();
			$tea_code = $currentSheet->getCell('K'.$currentRow)->getValue();
			$course = $currentSheet->getCell('I'.$currentRow)->getValue();
			
			$test1 = $trans->testClass($class_code);
			$test2 = $trans->testTeacher($tea_code);
			$str="<br>";
			if($test1){
				$test3 = $trans->testPeriod($test1, $section);
				if($test3){//已有课程，则需要更新课程，设置flag为覆盖导入。
					$flag = 2;
					$str .='警告：表格第'.$currentRow.'行记录课时已存在<br/>';
				}
			}else{
				$flag = -1;
				$str .='错误：表格第'.$currentRow.'行记录班级不存在,请先导入编码为 '.$class_code.' 的班级<br/>';
			}
			if(!$test2){
				$flag = -1;
				$str .='错误：表格第'.$currentRow.'行记录教师不存在,请先导入编码为 '.$tea_code.' 的教师<br/>';
			}
			$str .="<br>";
		}
		if(true){
			echo 'tran->section,class_code,tea_code,date,start_time,end_time,place<br>';
			for($currentRow = 2;$currentRow<$allRow;$currentRow++)
			{
				//加入数据验证。
				
				
				
				$section = $currentSheet->getCell('A'.$currentRow)->getValue();
				$course = $currentSheet->getCell('I'.$currentRow)->getValue();
				$class_code = $currentSheet->getCell('C'.$currentRow)->getValue();
				$tea_code = $currentSheet->getCell('K'.$currentRow)->getValue();
				$date = $currentSheet->getCell('E'.$currentRow)->getValue();
				$start_time = $currentSheet->getCell('F'.$currentRow)->getValue();
				$end_time = $currentSheet->getCell('G'.$currentRow)->getValue();
			
				$area = $currentSheet->getCell('AL'.$currentRow)->getValue();
				$classroom = $currentSheet->getCell('J'.$currentRow)->getValue();
				$place = $area.$classroom;
				
				$subjectType = mb_substr($course,-2,2);
				switch ($subjectType){
					case '数学':$subject_id = 101;break;
					case '物理':$subject_id = 102;break;
					case '化学':$subject_id = 103;break;
					case '英语':$subject_id = 104;break;
					case '政治':$subject_id = 105;break;
					case '地理':$subject_id = 106;break;
					case '历史':$subject_id = 107;break;
					case '语文':$subject_id = 108;break;
					case '生物':$subject_id = 109;break;
					default :$subject_id = 100;
				}
				if(strstr($course,'初')||strstr($course,'中考')){
					$subject_id += 100;
				}else if(strstr($course,'高')){
					$subject_id -= 100;
				}else{
					echo '………………未知的………………';
					echo $currentRow;
				}
				echo 'tran->'.$section.",".$class_code.",".$tea_code.",".$date.",".$start_time.",".$end_time.",".$place."  ";
				$result = $trans->addPeriod($course, $place, $section, $date, $start_time, $end_time, $class_code, $tea_code,$subject_id);
				echo $result.'<br/>';
			}

			echo $str;
		}
		$end = microtime(TRUE);
		echo '检测及执行消耗了'.($end-$begin).'秒<br/>';
	}

}